# !/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
sys.path.append("..")
import importlib
from modelDb.record import recordModel
from config.db import dspDb as dspConf
from modelDb.tourists import touristsModel
from modelDb.tourists_actions import touristsActionsModel
from modelDb.tourists_info import touristsInfoModel
import pymysql
from config.db import redis as redisConf
import redis
import json
import time



#=============================================================
# dmp etl
#=============================================================
class importDspMain(object):
    def __init__(self):
        """
        初始化
        """
        self.conn = pymysql.connect(
            host=dspConf.get("host"),
            port=dspConf.get("port"),
            user=dspConf.get("user"),
            passwd=dspConf.get("password"),
            db=dspConf.get("database"),
            charset="utf8"
        )

        # redis
        self.redisConn = redis.StrictRedis(
            host=redisConf.get("host"),
            port=redisConf.get("port"),
            db=redisConf.get("db"),
            password=redisConf.get("auth")
        )

        self.source = {
            1: "dsp",
            2: "taiyue",
        }

        self.modelEtl = None


    def start(self):
        """
        入口
        :return:
        """
        while True:
            try:
                sourceId = 1
                #todo 获取操作记录id
                self.touristsActions = touristsActionsModel()
                self.tourists = touristsModel()
                self.touristsInfoM = touristsInfoModel()
                self.touristsUser = {} #客源数据，优化查询，查询过之后不在查询
                self.touristsInfo = {}
                recordM = recordModel()
                record = recordM.findDsb()

                dspData = self.getDspData(record.get("update_id"))
                if not dspData:
                    time.sleep(60)
                    continue


                resultList = []
                dspId = 0
                for data in dspData:
                    try:
                        structure = self.getStructuredData(etlData=data, sourceId=sourceId, redisConn=self.redisConn)
                        self.touristsSelect(structure)
                        resultList.append(structure.getDictData())
                    except Exception, e:
                        print "================================="
                        print e
                        print json.dumps(data)
                    finally:
                        dspId = data.get('id')

                print "写入action"
                recordM.updateDsb(dspId)
                self.touristsActions.addMany(resultList)
                self.touristsActions = None
                self.tourists = None
                self.touristsUser = None
                print "操作完成"
            except Exception, e:
                print e

    def getStructuredData(self, etlData, sourceId, redisConn):
        """
        获取结构化数据
        :return:
        """
        data = etlData
        source = self.source[sourceId]
        if not self.modelEtl:
            etlModelPath = "modelEtl.%s" % (source)
            self.modelEtl = importlib.import_module(etlModelPath)

        etlClass = getattr(self.modelEtl, source)
        return etlClass(source=sourceId, data=data, redis=redisConn)


    def touristsSelect(self, structure):
        """
        客源管理
        :return:
        """
        phone = structure.phone
        if not self.touristsUser.has_key(phone):
            # todo 查询
            touristsData = self. tourists.findByPhone(phone)
            if not touristsData:
                structure.user_id = self.tourists.add(structure.getDictData())
                self.touristsInfoM.add(structure.getDictData())
                touristsData = {"id":structure.user_id, "etl_status":structure.etl_status}

            self.touristsInfo[phone] = self.touristsInfoM.findByPhone(phone)
            self.touristsUser[phone] = touristsData


        #客源详情
        touristsInfoData = self.touristsInfo[phone]
        self.touristsInfoM.saveImport(phone, touristsInfoData, structure.getDictData())

        touristsData = self.touristsUser[phone]
        structure.user_id = touristsData.get("id")

        return True

    # ====================================================
    # dsp数据
    # ====================================================
    def getDspData(self, id):
        cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

        sql = "select *  from collect_house_info where id>%d and spread like 'ia_%%' and username<>'' limit 100" %(id)
        print sql
        cursor.execute(sql)
        result = cursor.fetchall()

        cursor.close()
        return result


if __name__ == "__main__":
    importDspMain().start()
